# build separated user database
# Get build the User ID list

USER_DB_DIR = "./laptop_user_db/"
SEPARATE_USER_FILE = "./separate_users/laptop/"
GLOBAL_MAP = {}

module BuildSeparateUserDB
  def self.get_all_files
    Common.get_all_files(SEPARATE_USER_FILE)
    # "9.bcp"    
  end

  def self.handle_all_files(all_files)
  	puts "now start to build separated user database"

    counter = 0
    all_files.each do |file_name|
      counter = counter + 1
      handle_each_file file_name
      puts "-- " + file_name

      if counter % 10 == 0
        log = File.open "log.txt", "a"
        log.puts "#>[#{get_time}] close all database begin"

        puts "##> close all database"
        close_all_db
        GLOBAL_MAP.clear

        log.puts "#>[#{get_time}] close all database end"
        log.close
      end
    end
  end

  def self.get_time
    Time.now
  end
  
  def self.handle_each_file(file_name)
    log = File.open "build_userdb_log.txt", "a"
    puts ">> now handling : " + file_name

    log.puts ">>[#{get_time}] now handling : " + file_name
    
    file = File.open SEPARATE_USER_FILE + file_name, "r"

    begin
      while line = file.readline

        fields = line.split("|")
        client_ip = fields[5]
        db_key = (client_ip.split ".")[-1]

        # select the db to store the lines
        db = get_target_db db_key, file_name

        if db[client_ip]
          db[client_ip] = db[client_ip] + "}${" + line
        else
          db[client_ip] = line
        end

      end
    rescue Exception => e
      puts e.to_s
    end

    # to close the file
    file.close

    log.close

    close_all_db
    GLOBAL_MAP.clear
  end

  def self.get_target_db(key, file_name)
  	subdir = (file_name.split ".")[0]
  	unless FileTest.directory? USER_DB_DIR + subdir
  	  puts "mkdir: " + subdir
  	  Dir.mkdir USER_DB_DIR + subdir
  	end

    unless GLOBAL_MAP.has_key? key
      db = DBM.open(USER_DB_DIR  + subdir + "/" + subdir + "." + key)
      GLOBAL_MAP[key] = db
    end

    GLOBAL_MAP[key]
  end

  def self.close_all_db
    GLOBAL_MAP.each_value do |db|
      db.close
    end
  end

  def self.run
    puts ">> start to build user databases ..."

    begin
      all_files = BuildSeparateUserDB.get_all_files      
      all_facebook_id = BuildSeparateUserDB.handle_all_files all_files
      puts ">> end to build user dbs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end

end